home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Misc / RZToDoList / Source / ToDoList.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  1.0 KB  |  53 lines

  1. /* 
  2.  * ToDoList - list that sorts its elements based on the priority of the
  3.  *     ToDoItems that it contains
  4.  *
  5.  * You may freely copy, distribute and reuse the code in this example.
  6.  * This code is provided AS IS without warranty of any kind, expressed 
  7.  * or implied, as to its fitness for any particular use.
  8.  *
  9.  * Copyright 1995 Ralph Zazula (rzazula@next.com).  All Rights Reserved.
  10.  *
  11.  */
  12.  
  13. #import "ToDoList.h"
  14. #import "ToDoItem.h"
  15. #import <time.h>
  16. #import <stdlib.h>
  17.  
  18. @implementation ToDoList
  19.  
  20. static int toDoCompare(const void *x, const void *y)
  21. {
  22.     ToDoItem *xi = *(ToDoItem **)x;
  23.     ToDoItem *yi = *(ToDoItem **)y;
  24.     long xp = [xi priority];
  25.     long yp = [yi priority];
  26.     
  27.     /* sort by priority */
  28.     if(xp != yp) {
  29.         return yp - xp;
  30.     }
  31.     
  32.     /* items of similar priorty sort by creation date */
  33.     return [yi startDate] - [xi startDate];
  34. }
  35.  
  36. - sort
  37. {
  38.     qsort((ToDoItem **)dataPtr, numElements, sizeof(id), toDoCompare);
  39.     return self;
  40. }
  41.  
  42. - insertObject:anObject at:(unsigned)index
  43. {
  44.     if(![super insertObject:anObject at:index]) {
  45.         return nil;
  46.     }
  47.     [self sort];
  48.     return self;
  49. }
  50.  
  51. @end
  52.  
  53.